package com.etone.smartAudit.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    private final static SimpleDateFormat MY_DATE_FORMAT = new SimpleDateFormat("yyyy年MM月dd日");

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public final static String DF_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
    /**
     * yyyy-MM-dd HH:mm:ss.SSS
     */
    public final static String DF_yyyy_MM_dd_HH_mm_ss_SSS = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * yyyy-MM-dd
     */
    public final static String DF_yyyy_MM_dd = "yyyy-MM-dd";

    /**
     * yyyy年MM月dd日
     */
    public final static String DF_yyyy_MM_dd2 = "yyyy年MM月dd日";

    /**
     * HH:mm:ss
     */
    public final static String DF_HH_mm_ss = "HH:mm:ss";
    /**
     * MM-dd
     */
    public final static String DF_MM_dd = "MM-dd";
    /**
     * HH:mm:ss.SSS
     */
    public final static String DF_HH_mm_ss_SSS = "HH:mm:ss.SSS";

    /**
     * yyyyMM
     */
    public final static String DF_yyyyMM = "yyyyMM";
    /**
     * yyyyMMddHHmmssSSS
     */
    public final static String DF_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";

    /**
     * yyyyMMddHHmmss
     */
    public final static String DF_yyyyMMddHHmmss = "yyyyMMddHHmmss";

    /**
     * MM-dd mm:ss
     */
    public final static String DF_MM_dd_HH_mm = "MM-dd HH:mm";

    /**
     * yyyy-MM-dd HH:mm
     */
    public final static String DF_yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";

    /**
     * yyyy年MM月dd日 HH:mm
     */
    public final static String DF_yyyy_MM_dd_HH_mm2 = "yyyy年MM月dd日 HH:mm";

    /**
     * yyyy年MM月dd日 HH:mm:ss
     */
    public final static String DF_yyyy_MM_dd_HH_mm3 = "yyyy年MM月dd日 HH:mm:ss";

    /**
     * yyyy-M-d h:mm
     */
    public final static String DF_yyyy_M_d_h_mm = "yyyy-M-d h:mm";

    /**
     * yyyy年MM月
     */
    public final static String DF_yyyyYMMM = "yyyy年MM月";

    /**
     * yyyy年MM月
     */
    public final static String DF_yyyyMMdd = "yyyyMMdd";

    public static String fromDateH() {
        DateFormat format1 = new SimpleDateFormat(DF_yyyy_MM_dd_HH_mm_ss);
        return format1.format(new Date());
    }

    public static String fromDateToH(Date source) {
        DateFormat format1 = new SimpleDateFormat(DF_yyyy_MM_dd_HH_mm_ss);
        return format1.format(source);
    }

    public static String fromDateY() {
        DateFormat format1 = new SimpleDateFormat(DF_yyyy_MM_dd);
        return format1.format(new Date());
    }

    public static Date fromStringToDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DF_yyyy_MM_dd_HH_mm_ss);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间戳转date
     *
     * @param timestamp 时间戳
     * @return
     */
    public static String fromTimestampToDate(Long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat(DF_yyyy_MM_dd_HH_mm_ss);
        return sdf.format(new Date(timestamp));
    }

    public static Date getCurrDate() {
        Date date = new Date();

        DateFormat df2 = DateFormat.getDateTimeInstance();// 可以精确到时分秒
        try {
            return df2.parse(df2.format(date));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

    public static String getNiceDate(Date date) {
        String result = null;
        long currentTime = new Date().getTime() - date.getTime();
        int time = (int) (currentTime / 1000);
        if (time < 60) {
            result = "刚刚";
        } else if (time >= 60 && time < 3600) {
            result = time / 60 + "分钟前";
        } else if (time >= 3600 && time < 86400) {
            result = time / 3600 + "小时前";
        } else if (time >= 86400 && time < 864000) {
            result = time / 86400 + "天前";
        } else {
            result = parseDate2String(date, DF_MM_dd);
        }
        return result;
    }

    /**
     * 按指定的字符串格式将字符串型日期转化为java.util.Date型日期
     *
     * @param dateFormatType "yyyy-MM-dd" 或者 "yyyy-MM-dd hh:mm:ss"
     * @return java.util.Date型日期
     * @Param dateStr 字符型日期
     */
    public static Date turnStrDateToJavaUtilDate(String strDate, String dateFormatType) {

        Date javaUtilDate = null;
        DateFormat df = new SimpleDateFormat(dateFormatType);
        if (strDate != null && (!"".equals(strDate)) && dateFormatType != null && (!"".equals(dateFormatType))) {

            try {

                javaUtilDate = df.parse(strDate);
            } catch (ParseException e) {
                logger.error(e.getMessage());
            }
        }
        return javaUtilDate;
    }

    /**
     * 指定的字符串格式转换成另外一种时间格式
     *
     * @param date             需要被转换的时间字符串
     * @param dateFormatType   需要被转换的时间字符串格式
     * @param toDateFormatType 转换后的时间格式
     */
    public static String turnStrDateToStrDate(String date, String dateFormatType, String toDateFormatType) {
        Date parse = turnStrDateToJavaUtilDate(date, dateFormatType);
        return turnJavaUtilDateToStrDate(parse, toDateFormatType);
    }

    /**
     * <b>根据特定格式得到当前时间字符串<b><br/>
     *
     * @param dateFormatType
     * @return
     * @author Yung·Fu
     */
    public static String getCurrentStrDateBySpecifiedFormatType(String dateFormatType) {
        if (dateFormatType != null && !"".equals(dateFormatType)) {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormatType);
            return sdf.format(new Date());
        }
        return "";
    }

    /**
     * @return eg: 2012091811320043154
     * @Title: getCurrentDateTime16Str
     * @Description: 获取时间16位+4位随机数
     * @Author: FuYung
     * @Since: 2014年1月6日上午11:37:25
     */
    public static String getCurrentDateTime16Str() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssss");
        /* 生成随机数 */
        int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        Random rand = new Random();
        for (int i = 10; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < 4; i++) {
            result = result * 10 + array[i];
        }

        return sdf.format(new Date()) + result;
    }

    /**
     * @return
     * @Title: getCurrentDateTime16Str
     * @Description: 获取时间6位+2位随机数
     * @Author: FuYung
     */
    public static String getCurrentDateTime8Str() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        /* 生成随机数 */
        int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        Random rand = new Random();
        for (int i = 10; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < 2; i++) {
            result = result * 10 + array[i];
        }

        return sdf.format(new Date()) + result;
    }

    /**
     * @return
     * @Title: getCurrentDateTime8
     * @Description: 获取时间8位
     * @Author: FuYung
     */
    public static String getCurrentDateTime8() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        /* 生成随机数 */
        int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        Random rand = new Random();
        for (int i = 10; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }

        return sdf.format(new Date());
    }

    /**
     * @param javaUtilDate   java.util.Date,传入的java.util.Date型日期
     * @param dateFormatType "yyyy-MM-dd"或者<br>
     *                       "yyyy-MM-dd hh:mm:ss EE"或者<br>
     *                       "yyyy年MM月dd日 hh:mm:ss EE" <br>
     *                       (年月日 时:分:秒 星期 ，注意MM/mm大小写)
     * @return String 指定格式的字符串型日期
     * @Title: turnJavaUtilDateToStrDate
     * @Description: 将java.util.Date型日期转化指定格式的字符串型日期
     * @Author: FuYung
     * @Since: 2014年1月6日上午11:33:53
     */
    public static String turnJavaUtilDateToStrDate(Date javaUtilDate, String dateFormatType) {
        String strDate = "";
        if (javaUtilDate != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormatType);
            strDate = sdf.format(javaUtilDate);
        }
        return strDate;
    }

    /**
     * 辅助方法，用于比较两个字符串格式的日期的前后顺序
     *
     * @param dateStr1 日期1的字符串
     * @param dateStr2 日期2的字符串
     * @param fmt      日期1和日期2的各种，默认为"yyyy-MM-dd hh:mm"
     * @return 1代表日期1在日期2之后，-1代表日期1在日期2之前，0代表日期1和日期2相等
     */
    public static int compareDate(String dateStr1, String dateStr2, String fmt) {
        if (null == fmt || "".equals(fmt.trim())) {
            fmt = "yyyy-MM-dd hh:mm:ss";
        }
        // 先将日期字符串转换成日期格式，
        SimpleDateFormat formatDate = new SimpleDateFormat(fmt);
        try {
            Date date1 = formatDate.parse(dateStr1);
            Date date2 = formatDate.parse(dateStr2);
            return compareDate(date1, date2);
        } catch (ParseException e) {
            logger.error(e.getMessage());
            return 0;
        }
    }

    /**
     * 比较两个日期的前后顺序
     *
     * @param date1 日期1
     * @param date2 日期2
     * @return 1代表日期1在日期2之后，-1代表日期1在日期2之前，0代表日期1和日期2相等
     */
    public static int compareDate(Date date1, Date date2) {
        try {
            if (date1.getTime() > date2.getTime()) {
                return 1;
            } else if (date1.getTime() < date2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return 0;
    }

    /**
     * @param createTime 单位为秒
     * @return 返回java.util.Date（Sat Jan 04 14:50:17 CST 2014）
     * @Title: formatTimeReturnDate
     * @Description: 将微信中时间转换成标准格式的时间（yyyy-MM-dd HH:mm:ss）
     * @Author: FuYung
     * @Since: 2014年1月9日上午10:23:29
     */
    public static Date formatTimeReturnDate(String createTime) {
        // 将微信传入的CreateTime转换成long类型，再乘以1000
        long msgCreateTime = Long.parseLong(createTime) * 1000L;
        return new Date(msgCreateTime);
    }

    public static Date turnDateFormatType(Date date, String formatType) {
        String str = turnJavaUtilDateToStrDate(date, formatType);
        return turnStrDateToJavaUtilDate(str, formatType);
    }

    /**
     * 获得间隔日期（主要是间隔N周、间隔N天）
     *
     * @param specifiedStrDate 指定日期
     * @param dateForamtType   指定日期格式
     * @param intervalNum      间隔数（周或者天）
     * @param calenderParam    指定修改日期格式的属性
     *                         Calendar.WEEK_OF_MONTH（周）或者Calendar.DAY_OF_MONTH（天）
     * @return
     */
    public static String getIntervalStrDate(String specifiedStrDate, String dateForamtType, int intervalNum,
                                            int calenderParam) {
        if (specifiedStrDate == null) {
            return null;
        }
        if (specifiedStrDate.trim().equals("")) {
            return null;
        }

        DateFormat df = new SimpleDateFormat(dateForamtType);

        Calendar cal = Calendar.getInstance();
        cal.setTime(DateUtil.turnStrDateToJavaUtilDate(specifiedStrDate, dateForamtType));
        cal.add(calenderParam, intervalNum);
        return df.format(cal.getTime());
    }

    /**
     * 指定日期与当前日期比较是否在指定intervalNum时间内
     *
     * @param dateForamtType 指定日期格式
     * @param intervalNum    间隔数（周或者天）
     * @param calenderParam  指定修改日期格式的属性
     *                       Calendar.WEEK_OF_MONTH（周）或者Calendar.DAY_OF_MONTH（天）
     */
    public static int JudgeIsWithinIntervalNum(Date date, int intervalNum, String dateForamtType, int calenderParam) {

        DateFormat df = new SimpleDateFormat(dateForamtType);
        String specifiedStrDate = df.format(date);
        String intervalStrDate = getIntervalStrDate(specifiedStrDate, dateForamtType, intervalNum, calenderParam);
        Date currentDate = new Date();
        String currentDateStr = df.format(currentDate);

        int a = compareDate(intervalStrDate, currentDateStr, dateForamtType);
        return a;
    }

    /**
     * 取得两个时间段的时间间隔
     *
     * @param t1 时间1
     * @param t2 时间2
     * @return t2 与t1的间隔天数
     * @throws ParseException 如果输入的日期格式不是0000-00-00 格式抛出异常
     * @author color
     */
    public static int getBetweenDays(String t1, String t2) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        int betweenDays = 0;
        Date d1 = format.parse(t1);
        Date d2 = format.parse(t2);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        // 保证第二个时间一定大于第一个时间
        // if(c1.after(c2)) {
        // c1.setTime(d2);
        // c2.setTime(d1);
        // }
        int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
        betweenDays = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
        for (int i = 0; i < betweenYears; i++) {
            c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
            betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
        }
        return betweenDays;
    }

    public static long getBetweenMinute(String begin, String end) throws ParseException {
        int hour = 0;
        int minute = 0;
        long total_minute = 0;
        StringBuffer sb = new StringBuffer();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date begin_date = df.parse(begin);
            Date end_date = df.parse(end);

            total_minute = (end_date.getTime() - begin_date.getTime()) / (1000 * 60);

            hour = (int) total_minute / 60;
            minute = (int) total_minute % 60;

        } catch (ParseException e) {
            System.out.println("传入的时间格式不符合规定");
        }

        sb.append("工作时间为：").append(hour).append("小时").append(minute).append("分钟");

        System.out.println(sb);
        return hour * 60 + minute;
    }

    /**
     * 取得两个时间段的时间间隔（秒）
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return end 与begin的间隔秒数
     * @throws ParseException 如果输入的日期格式不是0000-00-00 格式抛出异常
     * @author yanbo
     */
    public static long getBetweenSeconds(String begin, String end) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(DF_yyyy_MM_dd_HH_mm_ss);
        long seconds = 0;
        try {
            Date begin_date = df.parse(begin);
            Date end_date = df.parse(end);

            seconds = (end_date.getTime() - begin_date.getTime()) / 1000;
        } catch (ParseException e) {
            System.out.println("传入的时间格式不符合规定");
        }
        return seconds;
    }

    /**
     * 取得两个时间段的时间间隔（秒）
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return end 与begin的间隔秒数
     * @author songkang
     */
    public static long getBetweenSeconds(Date begin, Date end) {

        long seconds = 0;

        Date begin_date = begin;
        Date end_date = end;

        seconds = (end_date.getTime() - begin_date.getTime()) / 1000;

        return seconds;
    }

    /**
     * 将日期类型的对象转换成字符串格式
     *
     * @param date 需要转换的日期对象
     * @param fmt  转换格式，例如"yyyy年MM月dd日 HH时mm分ss秒 E"；默认是"yyyy年MM月dd日"
     * @return 转换后的字符串格式日期
     */
    public static String parseDate2String(Date date, String fmt) {
        if (null == fmt || "".equals(fmt.trim())) {
            fmt = "yyyy年MM月dd日";
        }
        DateFormat df = new SimpleDateFormat(fmt);
        return df.format(date);
    }

    /**
     * 将日期字符串转换成Date类型
     *
     * @param dateStr 需要转换的日期字符串
     * @param fmt     转换格式，例如"yyyy年MM月dd日 HH时mm分ss秒 E"；默认是"yyyy-MM-dd"
     * @return
     */
    public static Date parseString2Date(String dateStr, String fmt) {
        if (null == fmt || "".equals(fmt.trim())) {
            fmt = "yyyy-MM-dd";
        }
        SimpleDateFormat df = new SimpleDateFormat(fmt);
        Date date = null;
        try {
            date = df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * UTC时间转时间对象
     *
     * @param dateStr 格式:yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
     * @return
     */
    public static Date parseUTC2Date(String dateStr) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = null;
        try {
            date = df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取上个月
     *
     * @param date
     * @return
     * @author szy
     */
    public static Date getLastDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);
        return cal.getTime();
    }

    /**
     * 获取当前时间的上个月
     *
     * @return
     * @author szy
     */
    public static Date getMonthDate() {
        return parseString2Date(DateUtil.parseDate2String(DateUtil.getLastDate(new Date()), "yyyy-MM"), "yyyy-MM");
    }

    /**
     * 功能描述：取得指定月份的第一天
     *
     * @param strdate String 字符型日期
     * @return String yyyy-MM-dd 格式
     */
    public static String getMonthBegin(String strdate) {
        Date date = parseString2Date(strdate, "yyyy-MM");
        return parseDate2String(date, "yyyy-MM") + "-01";
    }

    /**
     * 功能描述：取得指定月份的最后一天
     *
     * @param strdate String 字符型日期
     * @return String 日期字符串 yyyy-MM-dd格式
     */
    public static String getMonthEnd(String strdate) {
        Date date = parseString2Date(getMonthBegin(strdate), "yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        return parseDate2String(calendar.getTime(), "yyyy-MM-dd");
    }

    /**
     * 日期加一
     *
     * @param strdate
     * @return
     */
    public static String parseAdd1Date(String strdate) {
        Date date = parseString2Date(getMonthBegin(strdate), "yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, 1); // 日期分钟加1,Calendar.DATE(天),Calendar.HOUR(小时)
        return parseDate2String(calendar.getTime(), "yyyy-MM-dd");
    }

    /**
     * 日期加法
     *
     * @param date 原始日期
     * @param type 加的类型 Calendar.MONTH Calendar.YEAR
     * @param num  加的数目
     * @return
     */
    public static Date parseAddDate(Date date, int type, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(type, num); // 日期分钟加1,Calendar.DATE(天),Calendar.HOUR(小时)
        return calendar.getTime();
    }

    /**
     * 是否同月
     *
     * @param a
     * @param b
     * @return
     */
    public static boolean isSameMonth(Date a, Date b) {
        Boolean result = false;
        String dateA = turnJavaUtilDateToStrDate(a, "yyyy-MM-dd HH:mm:ss");
        String dateB = turnJavaUtilDateToStrDate(b, "yyyy-MM-dd HH:mm:ss");
        if (dateA.substring(0, 7).equals(dateB.substring(0, 7))) {
            result = true;
        }
        return result;
    }

    public static Date getRandomDateBetween(Date a, Date b) {
        try {
            if (a.getTime() >= b.getTime()) {
                return null;
            }
            long date = random(a.getTime(), b.getTime());
            Date r = new Date(date);
            @SuppressWarnings("deprecation")
            int hours = r.getHours();
            if (hours > 21 || hours < 8) {
                return null;
            }
            return new Date(date);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    private static long random(long begin, long end) {
        long rtnn = begin + (long) (Math.random() * (end - begin));
        if (rtnn == begin || rtnn == end) {
            return random(begin, end);
        }
        return rtnn;
    }

    /**
     * String date转为Timestamp 字符串
     *
     * @param date String date
     * @return 字符串
     */
    public static String turnDateToTimeStamp(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return String.valueOf(sdf.parse(date).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * java.util.date转为yyyyMMdd字符串
     *
     * @param date
     * @return
     */
    public static String turnDateToYMDString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return String.valueOf(sdf.format(date));
    }

    /**
     * java.util.date转为Timestamp 字符串
     *
     * @param date String date
     * @return 字符串
     */
    public static String turnDateToTimeStamp(Date date) {
        return String.valueOf(date.getTime());
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     */
    public static int daysBetween(Date smdate, Date bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);

            return Integer.parseInt(String.valueOf(between_days));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 字符串的日期格式的计算
     */
    public static int daysBetween(String smdate, String bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(sdf.parse(smdate));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);

            return Integer.parseInt(String.valueOf(between_days));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 得到 data之后的day天
     *
     * @param dt
     * @return
     */
    public static String getDataAddDays(Date dt, Integer day) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.DAY_OF_YEAR, day);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /**
     * 设置日期
     *
     * @param dt
     * @param day
     * @return
     */
    public static String setDateDay(Date dt, Integer day, Integer hh, Integer mm, Integer ss) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar ca = Calendar.getInstance();
        ca.setTime(dt);
        int year = ca.get(Calendar.YEAR);
        int month = ca.get(Calendar.MONTH);
        ca.set(year, month, day, hh, mm, ss);
        Date dt1 = ca.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /**
     * 判断指定日期是否今天
     *
     * @param date
     * @return true:是，false:否
     */
    public static boolean isToday(Date date) {
        Date begin = fromStringToDate(fromDateY() + " 00:00:00");
        Date end = fromStringToDate(fromDateY() + " 23:59:59");
        if (compareDate(date, begin) == 1 && compareDate(date, end) == -1) {
            return true;
        }
        return false;
    }

    /**
     * 根据时间获取是周几
     *
     * @param date 时间
     * @return 周几
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDaysName = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        //String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDaysName[intWeek];
    }

    /**
     * 取得当前日期是多少周
     *
     * @param date
     * @return
     */
    public static int getWeekOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);
        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 得到某一年周的总数
     *
     * @param year
     * @return
     */
    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        return getWeekOfYear(c.getTime());
    }

    /**
     * 得到某年某周的第一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);
        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);
        return getFirstDayOfWeek(cal.getTime());
    }

    /**
     * 得到某年某周的最后一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);
        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);
        return getLastDayOfWeek(cal.getTime());
    }

    /**
     * 取得指定日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }

    /**
     * 取得指定日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return c.getTime();
    }

    /**
     * 取得当前日期所在周的第一天
     *
     * @return
     */
    public static Date getFirstDayOfWeek(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        try {
            c.setTime(sdf.parse(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }

    /**
     * 取得当前日期所在周的最后一天
     *
     * @return
     */
    public static String getLastDayOfWeek(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        try {
            c.setTime(sdf.parse(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return sdf.format(c.getTime());
    }

    /**
     * 获取周数（格式yyyy-weeks）
     *
     * @param dateStr
     * @return
     */
    public static String getYearWork(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cl = Calendar.getInstance();
        try {
            cl.setTime(sdf.parse(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int week = cl.get(Calendar.WEEK_OF_YEAR);
        System.out.println(week);
        cl.add(Calendar.DAY_OF_MONTH, -7);
        int year = cl.get(Calendar.YEAR);
        if (week < cl.get(Calendar.WEEK_OF_YEAR)) {
            year += 1;
        }
        return year + "-" + week;
    }

    /**
     * 获取上一周周日日期
     *
     * @return
     */
    public static String getPreviousWeekOfSunday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, 1);
        return sdf.format(cal.getTime());
    }

    /***
     * 日期月份减指定月
     *
     * @param dateStr
     *            日期(2014-11)
     * @return 2014-10
     */
    public static String specifyMonth(String dateStr, int month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.MONTH, month);
        date = cl.getTime();
        return sdf.format(date);
    }

    public static String getReportPeriods() {
        Calendar c = Calendar.getInstance();
        int weeks = c.get(Calendar.WEEK_OF_MONTH);
        int month = c.get(Calendar.MONTH);
        int cYear = c.get(Calendar.YEAR);
        return cYear + "年" + month + "月" + "第" + weeks + "期";
    }
}
